--开机30秒后执行一次，检查三元组信息
--搭配后台服务，用于自动注册设备
module(...,package.seeall)

require "sys"
require "httpv2"
require "socket"

local RequestURL="https://XXXX4.XXXXi.fc.XXXXcs.com/XXXXX/"

function CheckDeviceName()
    while not socket.isReady() do sys.waitUntil("IP_READY_IND") end
    sys.wait(20000)
    local RequestBody={}
    RequestBody["IMEI"]=misc.getImei()
    RequestBody=json.encode(RequestBody)
    local Code,Header,Body = httpv2.request("POST",RequestURL,20000,nil,RequestBody,2)
    if Code=="200" then
        log.info("CheckDeviceName.Body = ",Body)
        local BodyJson,Result = json.decode(Body)
        if Result then
            if nvm.get("DeviceSecret") ~= BodyJson["DeviceSecret"] then 
                nvm.set("DeviceSecret",BodyJson["DeviceSecret"])
                nvm.set("ProductKey",BodyJson["ProductKey"])
                nvm.flush()
                log.info("CheckDeviceName.off = ","Power off")
                rtos.poweroff()
                return
            end
        end
    end
end

sys.taskInit(CheckDeviceName)

